home *** CD-ROM | disk | FTP | other *** search
/ Palm Utilities / Palm_Utilities_CD-ROM_2001_2001.iso / files / internet misc / GetTLE 1.0 / GetTLE.exe / Src / URLsMemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-26  |  8.2 KB  |  250 lines

  1. /*
  2.     GetTLE - URLsMemo.c - Handle the information in the GetTLE Memo
  3.     Copyright ⌐2000 Andreas Schneider
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. #include <PalmOS.h>
  21. #include "GetTLERsc.h"
  22. #include "URLsMemo.h"
  23. #include "alerts.h"
  24. #include "debug.h"
  25.  
  26. // Structures imposed by other programs
  27.  
  28. typedef struct {
  29.     char     note;        // null terminated
  30. } MemoDBRecordType, *MemoDBRecordPtr;
  31.  
  32. #define MemoDBName "MemoDB"
  33. #define MemoDBType 'DATA'
  34.  
  35. extern Char SiteUrls[MAX_SITES][MAX_SITE_URL_LEN+1]={0};
  36. extern Char SiteNames[MAX_SITES][MAX_SITE_NAME_LEN+1]={0};
  37. extern UInt16 NumSites=0;
  38.  
  39. // array of URLs containing TLE data
  40. // followed by a description of that site
  41. // each entry must end with \n
  42. // last entry must be NULL
  43. static Char *default_sites[]=
  44. {"www.celestrak.com/NORAD/elements/stations.txt\n",
  45.  "NORAD: Mir & ISS\n",
  46.  "www.celestrak.com/NORAD/elements/visual.txt\n",
  47.  "NORAD: visible satellites\n",
  48.  "www.celestrak.com/NORAD/elements/tle-new.txt\n",
  49.  "NORAD: new satellites\n",
  50.  "oig1.gsfc.nasa.gov/files/visible.tle\n",
  51.  "NASA: visible satellites\n",
  52.  "tie.jpl.nasa.gov/pub/dransom/current.data/stsnew.txt\n",
  53.  "Latest shuttle data\n",
  54.  "home.t-online.de/home/R.Kracht/bright50.tle\n",
  55.  "50+ brightest satellites\n", 
  56.  "www.wingar.demon.co.uk/satevo/tle/sts.tle\n",
  57.  "SatEvo: Shuttle\n", 
  58.  "www.wingar.demon.co.uk/satevo/tle/mir.tle\n",
  59.  "SatEvo: Mir\n",
  60.  "www.wingar.demon.co.uk/satevo/tle/iss.tle\n",
  61.  "SatEvo: ISS\n",
  62.  "www.wingar.demon.co.uk/satevo/tle/select.tle\n",
  63.  "SatEvo: selected satellites\n",
  64.  "www.fc.net/~mikem/ftp/visual.tle\n",
  65.  "Mike McCants' visible satellites\n",
  66.  NULL};
  67.  
  68. // store a site in the Sites array
  69. static void AddSite(Char *url,Char *name)
  70. {
  71.   // reality check
  72.   if (NumSites<MAX_SITES && StrLen(url)>0 && StrLen(name)>0)
  73.   {
  74.     // copy
  75.     StrCopy(SiteUrls[NumSites],url);
  76.     StrCopy(SiteNames[NumSites],name);
  77.     // keep track
  78.     NumSites++;
  79.     LogMessage("Site: %s\n",url);
  80.     LogMessage("Name: %s\n",name);
  81.   }
  82.   return;
  83. }
  84.  
  85. // returns true if we found our GetTLE Memo
  86. // needs the Ref of the Memo application database
  87. static Boolean HandleURLsMemo(DmOpenRef MemoDB)
  88. {
  89.   Char site_url[MAX_SITE_URL_LEN+1];
  90.   Char site_name[MAX_SITE_NAME_LEN+1];
  91.   UInt16 num_memos;
  92.   UInt16 record_number=0; // start with record 0
  93.   MemHandle record_handle=NULL;  
  94.   MemoDBRecordPtr record_ptr=NULL;  
  95.   Char *memo_ptr;
  96.   Char *string_ptr=site_url;
  97.   UInt16 len=0;
  98.   UInt16 slashes=0; // count slashes in site names 
  99.   Boolean reading_url=true;
  100.   Boolean found=false;
  101.  
  102.   // how many memos are there in the database
  103.   num_memos=DmNumRecordsInCategory(MemoDB,dmAllCategories);
  104.   do 
  105.   {
  106.     record_handle=DmQueryNextInCategory(MemoDB,&record_number,dmAllCategories);
  107.     if (record_handle)
  108.     {
  109.       // found another record (==memo)
  110.       // lock it so we can look at it
  111.       record_ptr=MemHandleLock(record_handle);
  112.       // does it start with the right string
  113.       if (StrStr(&(record_ptr->note),URLsMemoHeader)==&(record_ptr->note))
  114.       {    
  115.         found=true;
  116.         // found the right memo  
  117.         // Now we split the memo into lines, each representing 
  118.         // one site URL followed by a line containing a name.
  119.         // Start after the GetTLE\n string. As the
  120.         // memo is \0 terminated we know when to stop
  121.         // first we reaqd the url
  122.         reading_url=true;
  123.         for (memo_ptr=&(record_ptr->note)+StrLen(URLsMemoHeader);*memo_ptr;memo_ptr++)
  124.         {
  125.           // Look at each character
  126.           switch(*memo_ptr)
  127.           {
  128.             case '\n':
  129.               // found one complete line
  130.               // terminate the site string with \0
  131.               *string_ptr='\0';
  132.               // which string were we reading?
  133.               if (reading_url)
  134.               {
  135.                 // need to read the site name next
  136.                 site_name[0]='\0';
  137.                 string_ptr=site_name;
  138.                 reading_url=false;
  139.               }
  140.               else
  141.               {
  142.                 // so now we've finished the site name
  143.                 // store the site's url and name
  144.                 AddSite(site_url,site_name);
  145.                 // get ready to read the next site url
  146.                 site_url[0]='\0';
  147.                 string_ptr=site_url;
  148.                 reading_url=true;
  149.               }
  150.               len=0;
  151.               break;
  152.             default:
  153.               // everything else is part of the URL
  154.               // up to a certain length of course
  155.               if (len<MAX_SITE_URL_LEN)
  156.               {
  157.                 if (reading_url==false && (*memo_ptr)=='/')
  158.                 {
  159.                   slashes++;
  160.                 }
  161.                 *string_ptr=*memo_ptr;
  162.                 string_ptr++;
  163.                 len++;
  164.               }
  165.               break;
  166.           }
  167.         }
  168.         // end of memo, but the last site might not
  169.         // have been \n terminated, so do the stuff for
  170.         // the case '\n' now.
  171.         *string_ptr='\0';
  172.         AddSite(site_url,site_name);
  173.       }
  174.       // unlock the memo - we're finished with it
  175.       MemHandleUnlock(record_handle);
  176.     }
  177.     record_number++; // continue search for Memo one record higher
  178.   } while (record_handle!=NULL && !found);
  179.   // warn if the memo looks like an old one...
  180.   if (slashes>3) // some low number
  181.   {
  182.     MyStatusFunc("GetTLE memo looks outdated. If you have problems, delete it and retry.");
  183.   }
  184.   return found;
  185. }
  186.  
  187. // read (and maybe create) the GetTLE memo
  188. extern void ReadURLsMemo(void)
  189. {
  190.   DmOpenRef MemoDB=0;
  191.   MemoDBRecordPtr record_ptr=NULL;  
  192.   MemHandle record_handle=NULL;
  193.   Boolean found=false;
  194.   UInt32 length;
  195.   UInt32 offset;
  196.   UInt16 index;
  197.       
  198.   LogMessage("Find Memo: %s",URLsMemoHeader);
  199.   // open the database of the built in Memo application
  200.   MemoDB = DmOpenDatabaseByTypeCreator(MemoDBType, 'memo', dmModeReadWrite);
  201.   if (MemoDB)
  202.   {
  203.     // found it - as expected
  204.     // now try to read URLs from the GetTLE memo
  205.     found=HandleURLsMemo(MemoDB);
  206.     if (!found)
  207.     {
  208.       // there is no GetTLE Memo yet - create one
  209.       // and put the default sites into it
  210.       // first find out how long the text is we are going to write
  211.       length=StrLen(URLsMemoHeader); // includes \n
  212.       for (index=0;default_sites[index]!=NULL;index++)
  213.       {
  214.         length+=StrLen(default_sites[index]);
  215.       }
  216.       length++; // the trailing \0
  217.       // allocate memory
  218.       record_handle=(MemHandle)DmNewHandle(MemoDB,length);
  219.       if (record_handle!=NULL)
  220.       {
  221.         // lock the handle, so we can write to it
  222.         record_ptr=MemHandleLock(record_handle);
  223.         // first write the header
  224.         offset=0;
  225.         DmStrCopy(record_ptr,offset,URLsMemoHeader);
  226.         offset+=StrLen(URLsMemoHeader);
  227.         // then write the URLs
  228.         for (index=0;default_sites[index]!=NULL;index++)
  229.         {
  230.           DmStrCopy(record_ptr,offset,default_sites[index]);
  231.           offset+=StrLen(default_sites[index]);
  232.         }
  233.         // now we've got the data in the record.
  234.         // first we release it
  235.         MemPtrUnlock(record_ptr);
  236.         // and then we attach it to the Memo database
  237.         index=1000; // attach to end of database
  238.         if (DmAttachRecord(MemoDB,&index,record_handle,0))
  239.         {
  240.           // record is in database - free used memory
  241.           MemHandleFree(record_handle);
  242.         }
  243.         // now there is a GetTLE memo - use it
  244.         HandleURLsMemo(MemoDB);
  245.       }
  246.     }
  247.     DmCloseDatabase(MemoDB);
  248.   }
  249.   return;
  250. }